<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
          + request.getServerName() + ":" + request.getServerPort()
          + path + "/";
%>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>登录</title>
  <link rel="stylesheet" href="<%=path%>/css/aem-h5/aem-member-login-style.css">
  <script src="<%=path%>/js/aem/aem-member-login-rem.js"></script>
</head>
<body>
  <div class="container">
    <img class="tip" src="<%=path%>/images/aem/tip.png">
    <div class="form-group">
      <div class="key">手机号:</div>
      <div class="value">
        <input id="phone" type="text" placeholder="输入手机号">
      </div>
    </div>
    <div class="form-group phone">
      <div class="key">验证码:</div>
      <div class="value">
        <input class="code" id="code" type="text" placeholder="短信验证码">
        <button type="button" id="sendCode">发送验证码</button>
      </div>
    </div>
    <div class="login-btn" id="next">
      登 录
    </div>
  </div>
  <form action="" method="get" id = "myfrom">
    <input hidden="hidden" id="account" name="phone" value=""/>
    <input hidden="hidden" id="cid" name="cid" value=""/>
    <input hidden="hidden" id="thirdOpenId" name="thirdOpenId" value=""/>
  </form>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="<%=path%>/js/aem/aem-member-login.js"></script>
</body>
<script type="text/javascript">

    //发送验证码
    function  sendCodeAjax(){
        var account = $('#phone').val();
        var code_type = "no_unlimited";
        var type = "recover";
        var url = "<%=basePath%>/smsSend/ajaxSendCode";
        $.post(url, {"account": account, "codeType": code_type,"type":type}, function (data) {
            if (data.status == "200") {
                alert("验证码发送成功");
            } else if (data.status == "501") {
                alert(data.message);
            }
        }, "json");

    }

    //验证验证码
    $('#next').click(function () {
        var regPhone = $("#phone").val();
        var regCodeType = $('#code').val();
        if (regPhone == null || regPhone == "") {
            alert("请输入手机号！");
            return;
        }else if (!/^1[34578]\d{9}$/.test(regPhone)) {
            alert("手机格式错误，请重新输入！");
            return;
        }else if(regCodeType == null || regCodeType ==""){
            alert("请输入验证码！");
            return;
        }
        var account = $('#phone').val();
        var code_type = "change_code";
        var ver_code = $('#code').val();
        var params = {
            "account": account,
            "codeType": "no_unlimited",
            "verCode": ver_code
        }
        /* 先进行验证码校验 ，校验成功进行下一步 */
        var url = "<%=basePath%>/smsSend/ajaxSmsVerify";
        $.post(url, params, function (data) {
            if ("200" == data.status) {
                /* 校验成功，改变url，执行下一步 */
                //获取phone、openId、cid
                var openId = getQueryString("openId");
                var cid = getQueryString("cid");
                //调用ajax方法，判断手机号码是否已注册会员，
                // 已注册，解除该openId之前的手机号绑定，更新当前手机号为openId
                // 未注册，手机号码注册新会员，入会员库，会员姓名：调取手机号码，并绑定openId信息
                // 根据会员id、cid重定向到发券url

                var url = "<%=basePath%>/member/ajaxAemMemberPhone";
                $("#account").val(account);
                $("#cid").val(cid);
                $("#thirdOpenId").val(openId);

                $("#myfrom").attr("action",url);
                $('#myfrom').submit();
            } else {
                alert(data.message);
            }
        }, "json");


    });

    //获取字符串之后的值
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }


</script>
</html>